﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZLoadingData;
	import com.wandian.util.CLZTabBar;
	import com.wandian.view.components.ui.Gift;
	import com.wandian.view.components.ui.PlayerInfoMc;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

	/**
	 * 好友列表房间玩家信息tabs
	 * @author long
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class FriendAndRoomPlayerMediator extends Mediator
	{
		private var _minTabBar:CLZTabBar;
		private var _contenMc:Sprite;
		private var _arrShowPlayer:Array;
		private var _nRecordRoomId:int;
		private var _global:GameData;
		private var listPanel:CLZLlistScrollPanel;
		public static const NAME:String="FriendAndRoomPlayerMediator";

		public function FriendAndRoomPlayerMediator(viewComponent:Object=null)
		{
			this._global=GameData.getInstance();
			super(NAME, viewComponent);
			return;
		}

		override public function onRegister():void
		{
			this._contenMc=new Sprite();
			this.mcTabMain.addChild(this._contenMc);
			if (GameData.is800)
			{
				this._contenMc.x=600;
				this._contenMc.y=280;
			}
			else
			{
				this._contenMc.x=730;
				this._contenMc.y=303;
			}
			var tabs:Array=[[this.mcTabMain.friend_mc, GameData.tip["FRIEND_NAME"]], [this.mcTabMain.roomPlayer_mc, GameData.tip["ROOM_PLAYER"]]];
			this._minTabBar=new CLZTabBar(tabs);
			var _loc_2:*=tabs.length;
			this.mcTabMain.friend_mc.addEventListener(Event.CHANGE, this.showFriendHandler);
			this.mcTabMain.roomPlayer_mc.addEventListener(Event.CHANGE, this.showRoomPlayerHandler);
			this._minTabBar.setClickIndex(this.mcTabMain["friend_mc"]);
			return;
		}

		private function showFriendHandler(event:Event=null):void
		{
			this.removeAll();
			var userInfo:UserInfoProxy=facade.retrieveProxy(UserInfoProxy.NAME) as UserInfoProxy;
			this.showList(userInfo.arrFriend, CLZNoticeName.FRIEND_LIST_RES);
			return;
		}

		private function showRoomPlayerHandler(event:Event=null):void
		{
			this.removeAll();
			if (this._nRecordRoomId > 0)
			{
				CLZLoadingData.show(this._contenMc, 190, 305);
				this.sendNotification(CLZNoticeName.SHOW_ROOM_PALYER_LIST_REQ, this._nRecordRoomId);
			}
			return;
		}

		/**
		 *
		 * @param arrRoomUsers
		 * @param notifyName
		 */
		private function showList(arrRoomUsers:Array, notifyName:String):void
		{
			var total:int=0;
			var mcInvitation:MovieClip=null;
			var friendVo:FriendVo=null;
			var mcPlayerInfo:PlayerInfoMc=null;
			if (arrRoomUsers == null)
			{
				return;
			}
			this.removeAll();
			arrRoomUsers=GameData.sortMaxToMinArray(arrRoomUsers, "state");
			if (GameData.is800)
			{
				this.listPanel=new CLZLlistScrollPanel(1, 195, 61, 0, 0, 305, 170, 0);
			}
			else
			{
				this.listPanel=new CLZLlistScrollPanel(1, 195, 61, 0, 0, 305, 190, 0);
			}

			this._contenMc.addChild(this.listPanel);
			/*var _loc_4:Boolean = false;
			if (notifyName == CLZNoticeName.FRIEND_LIST_RES)
			{
				if (this._global.userInfo.userVo.gp != GameData.QZONE_SID)
				{
				}
			}
			if (this._global.userInfo.userVo.gp == GameData.FRIEND_SID)
			{
				_loc_4 = true;
			}
			else
			{
				_loc_4 = false;
			}
			if (_loc_4)
			{
				total = arrRoomUsers.length + 1;
			}
			else
			{
				total = arrRoomUsers.length;
			}*/
			total=arrRoomUsers.length;
			var count:int=0;
			while (count < total)
			{

				mcInvitation=GameData.getMovieClip("PlayerInfoMc");
//				this.listPanel.setMc(mcInvitation, count, total);
				/*if (_loc_4)
				{
					friendVo = arrRoomUsers[(count - 1)] as FriendVo;
				}
				else
				{
					friendVo = arrRoomUsers[count] as FriendVo;
				}
				if (friendVo == null)
				{
				}
				else
				{
					mcPlayerInfo = new PlayerInfoMc(mcInvitation, friendVo, notifyName, this._nRecordRoomId);
				}*/
				friendVo=arrRoomUsers[count] as FriendVo;
				mcPlayerInfo=new PlayerInfoMc(mcInvitation, friendVo, notifyName, this._nRecordRoomId);
				this.listPanel.setMc(mcInvitation, count, total);
				count=count + 1;

			}
			return;
		}

		/**
		 *
		 * @param arrRoomUsers
		 * @param notifyName
		 */
		private function showList0(arrRoomUsers:Array, notifyName:String):void
		{
			var total:int=0;
			var mcInvitation:MovieClip=null;
			var friendVo:FriendVo=null;
			var mcPlayerInfo:PlayerInfoMc=null;
			if (arrRoomUsers == null)
			{
				return;
			}
			this.removeAll();
			arrRoomUsers=GameData.sortMaxToMinArray(arrRoomUsers, "state");
			this.listPanel=new CLZLlistScrollPanel(1, 195, 61, 0, 0, 305, 190, 0);
			this._contenMc.addChild(this.listPanel);
			var _loc_4:Boolean=false;
			if (notifyName == CLZNoticeName.FRIEND_LIST_RES)
			{
				if (this._global.userInfo.userVo.gp != GameData.QZONE_SID)
				{
				}
			}
			if (this._global.userInfo.userVo.gp == GameData.FRIEND_SID)
			{
				_loc_4=true;
			}
			else
			{
				_loc_4=false;
			}
			if (_loc_4)
			{
				total=arrRoomUsers.length + 1;
			}
			else
			{
				total=arrRoomUsers.length;
			}
			var _loc_5:int=0;
			while (_loc_5 < total)
			{

				if (_loc_4)
				{
					if (_loc_5 == 0)
					{
						mcInvitation=GameData.getMovieClip("InvitationPlayer");
						this.listPanel.setMc(mcInvitation, _loc_5, total);
						CLZButton.setButton(mcInvitation);
						mcInvitation.addEventListener(MouseEvent.CLICK, this.onFriendHandler);
						;
					}
				}
				mcInvitation=GameData.getMovieClip("PlayerInfoMc");
				this.listPanel.setMc(mcInvitation, _loc_5, total);
				if (_loc_4)
				{
					friendVo=arrRoomUsers[(_loc_5 - 1)] as FriendVo;
				}
				else
				{
					friendVo=arrRoomUsers[_loc_5] as FriendVo;
				}
				if (friendVo == null)
				{
				}
				else
				{
					mcPlayerInfo=new PlayerInfoMc(mcInvitation, friendVo, notifyName, this._nRecordRoomId);
				}
				_loc_5=_loc_5 + 1;
			}
			return;
		}

		private function onFriendHandler(event:MouseEvent):void
		{
//            JSFUN.shareInvitationFunc();
			return;
		}

		private function removeAll():void
		{
			if (this.listPanel != null)
			{
				this.listPanel.remove();
				this.listPanel=null;
			}
			while (this._contenMc.numChildren)
			{

				this._contenMc.removeChildAt(0);
			}
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.ON_CHANGE_ROOM_ID, CLZNoticeName.FRIEND_LIST_RES, CLZNoticeName.UPDATE_FRIEND_STATE, CLZNoticeName.SHOW_ROOM_PALYER_LIST_RES, CLZNoticeName.GET_ROOM_LIST];
		}

		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case CLZNoticeName.UPDATE_FRIEND_STATE:
				{
					this._minTabBar.setClickIndex(this.mcTabMain["friend_mc"]);
					break;
				}
				case CLZNoticeName.FRIEND_LIST_RES:
				{
					trace("收到好友列表");
					this._minTabBar.setClickIndex(this.mcTabMain["friend_mc"]);
					break;
				}
				case CLZNoticeName.SHOW_ROOM_PALYER_LIST_RES:
				{
					CLZLoadingData.hide(this._contenMc);
					var arrRoomUsers:Array=notification.getBody() as Array; //GameData.sortMaxToMinArray(notification.getBody() as Array, "state");
					this.showList(arrRoomUsers, CLZNoticeName.SHOW_ROOM_PALYER_LIST_RES);
					break;
				}
				case CLZNoticeName.ON_CHANGE_ROOM_ID:
				{
					this._nRecordRoomId=notification.getBody() as int;
//					this._minTabBar.setClickIndex(this.mcTabMain["roomPlayer_mc"]);
					break;
				}
				case CLZNoticeName.GET_ROOM_LIST:
				{
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function get mcTabMain():Object
		{
			return viewComponent;
		}

	}
}
